<?php
namespace yunpian;

class Yunpian{
    private $apikey = '209e05c806a6a88d4aa9cdf2e9d32098';
    private $tpl_id ='2216728';
    public function __construct()
    {
        header("Content-Type:text/html;charset=utf-8");

    }

    /************************************************************************************/
//获得账户
    function get_user()
    {
        $url = 'https://sms.yunpian.com/v2/user/get.json';
        $data = ['apikey'=>$this->apikey];
        $result = $this->post($url,$data);
        return $result;
    }

    function send($text,$mobile)
    {
        $url = 'https://sms.yunpian.com/v2/sms/single_send.json';
        $data = array('text' => $text, 'apikey' => $this->apikey, 'mobile' => $mobile);
        $result = $this->post($url,$data);
        return $result;
    }

    function tpl_send($param = array(),$mobile,$tpl_id ='2216728')
    {
        if($param == array()){
            return ['code' => 201,'msg'=>'参数错误'];
        }
        $keys = array_keys($param);
        $str = '';
        foreach ($keys as $k =>$v){
            if($k == 0){
                $str.='#'.urlencode($v).'#'.'='.urlencode($param[$v]);
            }else{
                $str.='&'.'#'.urlencode($v).'#'.'='.urlencode($param[$v]);
            }
        }
        $url = 'https://sms.yunpian.com/v2/sms/tpl_single_send.json';
        $data = array('tpl_id' => $tpl_id, 'tpl_value' => $str, 'apikey' =>$this->apikey, 'mobile' => $mobile);
        $result = $this->post($url,$data);
        return $result;
    }

    function voice_send($code,$mobile){

        $url = 'http://voice.yunpian.com/v2/voice/send.json';
        $data = array('code' => $code, 'apikey' => $this->apikey, 'mobile' => $mobile);
        $result = $this->post($url,$data);
        return $result;
    }

    function notify_send($param,$tpl_id,$mobile)
    {
        $keys = array_keys($param);
        $str = '';
        foreach ($keys as $k =>$v){
            if($k == 0){
                $str.='#'.$v.'#'.'='.$param[$v];
            }else{
                $str.='&'.'#'.$v.'#'.'='.$param[$v];
            }
        }
        $url ='https://voice.yunpian.com/v2/voice/tpl_notify.json';
        $data = array('tpl_id' => $tpl_id, 'tpl_value' => urlencode($str), 'apikey' => $this->apikey, 'mobile' => $mobile);
        $result = $this->post($url,$data);
        return $result;
    }

    function checkErr($result, $error)
    {
        if ($result === false) {
            $arr = [
                'code' => 110,
                'msg' =>'请求错误'
                ];
        } else {
            $arr = ['code'=>200];
            //echo '操作完成没有任何错误';
        }
        return $arr;
    }

    function post($url,$data){
        $ch = curl_init();

        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $check = $this->checkErr($result, $error);
        if($check['code'] == 110){
            $result = $check;
        }else{
            $result = json_decode($result,true);
            if($result['code'] == 0){
                $result['code'] = '000000';
            }
            $result = json_encode($result);
        }
        return $result;
    }
}